/*
 file: event.js
 dependsOn: core.js, utils.js, string.js
 */

/**
 * Serializes objects into JSON format. Note that this method skips the 
 * serialization of functions, elements (including DOM nodes and Elements),
 * and undefined properties. If you pass one of these items to this
 * function directly, you will receive an empty string. Some notes:
 *   - null is serialized as 'null'
 *   - Dates, Numbers and Booleans are serialized using toString()
 *   - Strings are wrapped in single-quotes and properly escaped
 *   - Objects with a toJSON method are serialized by invoking that
 * @param  {Object} object The object to serialize
 * @return {String} The serialized object in JSON format
 */
$.toJSON = function(object) 
{
  var type = typeof object, results = [], property, value;
  
  switch (type) {
    case 'undefined': 
    case 'function': 
      return '';
    case 'boolean': 
    case 'number': 
      return String(object);
  }
  
  if (!object) 
    return 'null';
  if (object.toJSON) 
    return object.toJSON();
  if ($.isElement(object)) 
    return '';

  for (property in object) 
    if (!(value = $.toJSON(object[property]).empty()))
      results.push(property.toJSON() + ': ' + value);

  return '{' + results.join(',') + '}';
};
  